const API_KEY = "pat_GhkLNk1CvoxqqFpL7Xu4SF7cFSjZ3tFNe7qcj34dAVdNhqg9uVzezFcH4o18990J";
const BOT_ID = "7503096882806571043";

// 获取当前登录用户的ID
const getCurrentUserId = () => {
  const userEmail = localStorage.getItem('userEmail');
  return userEmail || 'nologin'; // 如果没有登录用户，返回默认ID
};

// 导出API配置
export const aiChatConfig = {
  apiKey: API_KEY,
  botId: BOT_ID,
  getUserId: getCurrentUserId
};

const headers = {
  'Authorization': `Bearer ${API_KEY}`,
  'Content-Type': 'application/json',
  'Accept': 'application/json'
};

// 创建对话api
export const createChat = async (additionalMessages) => {
  const response = await fetch("https://api.coze.cn/v3/chat", {
    method: "POST",
    headers: headers,
    body: JSON.stringify({
      bot_id: BOT_ID,
      user_id: getCurrentUserId(),
      stream: false,
      additional_messages: additionalMessages
    })
  });
  return await response.json();
};

// 获取消息列表api
export const getMessageList = async (conversationId, chatId) => {
  const response = await fetch(
    `https://api.coze.cn/v3/chat/message/list?conversation_id=${conversationId}&chat_id=${chatId}`,
    { headers }
  );
  return await response.json();
};
